//
//  WLLComposeTextView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WLLComposeTextView: UITextView {
        //重写 textView font 属性
   override var font: UIFont? {
        didSet{
            //设置占位文字的 font
            placeholderLabel.font = font
        }
    }
    //显示的占位文字
    var  placeholder : String? {
        didSet{
            //设置占位文字的 text
            placeholderLabel.text = placeholder
        }
    }
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     //MARK: - 设置视图
    func setupUI()  {
        
        backgroundColor = UIColor.whiteColor()
        // 添加控件
     addSubview(placeholderLabel)
        //设置约束
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(KSCREENW-10)
            
        }
        //监听系统通知 查看 textView 的文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
        
        
    }
    //监听文字改变
    func textViewTextDidChange() {
        placeholderLabel.hidden = self.hasText()
    }
    
    
     //MARK: - 懒加载控件
    //占位文字
    private lazy var placeholderLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
        return lab
    }()
    
    //析构函数
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
